package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;











    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRelation(id);
    }

    @Override
    public void save(Role role, Long[] ids) {
        roleMapper.insert(role);
        //判断是否有权限这个数组
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                roleMapper.insertRelation(role.getId(), id);
            }
        }
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRelation(role.getId());
        //判断是否有权限这个数组
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                roleMapper.insertRelation(role.getId(), id);
            }
        }
    }

    @Override
    public PageInfo list(QueryObject queryObject) {
        int currentPage = queryObject.getCurrentPage();
        int pageSize = queryObject.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<Role> list = roleMapper.list(queryObject);
        return new PageInfo(list);
    }
}

